<template>
  <div>
    <div class="theHead">
      <div class="centerBox">
        <div class="logo" @click="goHome">LOGO</div>
        <ul class="nav">
          <li
            v-for="(item, i) in checkTab"
            :key="i"
            :class="['navItem', { activeNav: i === tabIndex }]"
            @click="checkTabFn(i)"
          >
            {{ item.label }}
          </li>
        </ul>
        <input class="search" type="text" placeholder="请输入关键词" />
        <div v-if="this.$store.getters.getToken === ''" class="userInfo">
          <span class="login" @click="login"> 登录</span>
          <span class="register" @click="register">注册</span>
        </div>
        <div v-else class="userInfo">
          {{ this.$store.getters.getUserInfo.userEmail }}
          <span class="login" @click="outLogin">退出登录</span>
          <span class="login" @click="goSelfCneter">个人中心</span>
        </div>
      </div>
    </div>
    <transition name="transitionMove">
      <router-view />
    </transition>
  </div>
</template>

<script>
export default {
  name: 'TheHead',
  data() {
    return {
      ifLogin: false,
      checkTab: [
        { label: '首页', url: '/' },
        { label: '题库', url: '/questions' },
        { label: '面试经验', url: '/interviewArticle' },
        { label: '联系我们', url: '/3' }
      ],
      tabIndex: -1
    }
  },
  methods: {
    // 去首页
    goHome() {
      this.$router.push('/')
    },
    // 选择不同的顶部标签
    checkTabFn(val) {
      this.tabIndex = val
      const url = this.checkTab[val].url
      this.$router.push(url)
    },
    // 点击登录按钮
    login() {
      this.$router.push('/login')
    },
    // 点击注册按钮
    register() {
      this.$router.push('/register')
    },
    // 点击退出登录按钮
    outLogin() {
      this.$store.commit('deleteToken')
      this.$store.commit('deleteUser')
      this.$message({
        message: '退出登录',
        type: 'success'
      })
      this.$router.push('/')
    },
    goSelfCneter() {
      this.$router.push('/selfcenter')
    }
  },
  watch: {
    '$route.path': {
      handler(toPath, fromPath) {
        const urlList = this.checkTab.map((item) => item.url)
        if (urlList.includes(toPath)) {
          this.tabIndex = urlList.indexOf(toPath)
        } else {
          this.tabIndex = -1
        }
      },
      deep: true, // 深度监听
      immediate: true // 第一次初始化渲染就可以监听到
    }
  }
}
</script>

<style lang="scss" scoped>
.transitionMove-enter-active,
.transitionMove-leave-active {
  transition: all 0.5s cubic-bezier(1, 0.5, 0.8, 1);
}
.transitionMove-enter, .transitionMove-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
.theHead {
  height: 66px;
  background-color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  .centerBox {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    line-height: 66px;
    .logo {
      font-weight: 900;
      font-size: 24px;
      cursor: pointer;
    }
    .nav {
      .activeNav {
        border-bottom: 4px solid #00aae6;
        color: #00aae6;
      }
      .navItem {
        display: inline-block;
        line-height: 58px;
        margin-left: 66px;
        font-size: 14px;
        cursor: pointer;

        &:hover {
          color: #00aae6;
          transition: color 0.5s;
        }
      }
    }
    .search {
      width: 200px;
      height: 32px;
      padding-left: 12px;
      border: none;
      border-radius: 4px;
      background-color: #f4f8fa;
      margin-top: 18px;
      font-size: 12px;
      color: #f4f8fa;
    }
    .userInfo {
      font-size: 14px;
      .login {
        margin-left: 10px;
        cursor: pointer;
        &:hover {
          color: #00aae6;
          transition-duration: 0.5s;
        }
      }
      .register {
        margin-left: 10px;
        cursor: pointer;
        &:hover {
          color: #00aae6;
          transition-duration: 0.5s;
        }
      }
    }
  }
}
</style>